<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Area as AreaModel;
use DB;
use Illuminate\Database\Eloquent\Model;

class SystemSettings extends Model
{
    public $timestamps = false;
    protected $table   = 'system_settings';

    protected $fillable = [
        'config_name',
        'config_value',
    ];

    public function updOpenCities(string $areas)
    {
        $areas = (array) array_map(function ($item) {
            return intval(trim($item));
        }, (array) explode(',', $areas));
        $cityAreasRes     = AreaModel::select(['id'])->whereIn('id', $areas)->where(['level' => 1])->get();
        $districtAreasRes = AreaModel::select(['id'])->whereIn('id', $areas)->where(['level' => 2])->get();
        $rootAreas        = $cityAreasRes ? array_column($cityAreasRes->toArray(), 'id') : [];
        $childAreas       = $districtAreasRes ? array_column($districtAreasRes->toArray(), 'id') : [];

        $parentAreas = AreaModel::select(['parentid'])->whereIn('id', $childAreas)->where(['level' => 2])->get();
        $parentAreas = $parentAreas ? array_column($parentAreas->toArray(), 'parentid') : [];
        $rootAreas   = array_unique(array_merge((array) $rootAreas, (array) $parentAreas));

        $upd1                  = $upd2                  = $upd3                  = $upd4                  = false;
        $openRootAreaIdString  = implode(',', array_filter($rootAreas));
        $openChildAreaIdString = implode(',', array_filter($childAreas));
        $baseSql1              = 'UPDATE `area` AS a1 
            LEFT JOIN `area` AS a2 ON a1.`id`=a2.`parentid` 
            LEFT JOIN `area` AS a3 ON a2.`id` = a3.`parentid` ';
        $sql1 = $baseSql1 . ' SET a1.`is_open` = 1 ';
        $sql2 = $baseSql1 . ' SET a1.`is_open` = 0 ';
        if ($openRootAreaIdString) {
            $sql1 .= ' WHERE a1.`id` IN ( ' . $openRootAreaIdString . ' ) ';
            $sql2 .= ' WHERE a1.`id` NOT IN ( ' . $openRootAreaIdString . ' ) ';
        }
        $baseSql2 = 'UPDATE `area` AS a1 
            LEFT JOIN `area` AS a2 ON a1.`id`=a2.`parentid` 
            LEFT JOIN `area` AS a3 ON a2.`id` = a3.`parentid` ';
        $sql3 = $baseSql2 . ' SET a2.`is_open` = 1, a3.`is_open` = 1 ';
        $sql4 = $baseSql2 . ' SET a2.`is_open` = 0, a3.`is_open` = 0 ';
        if ($openChildAreaIdString) {
            $sql3 .= ' WHERE a1.parentid = 0 AND a1.level = 1 AND a2.`id` IN ( ' . $openChildAreaIdString . ' ) ';
            $sql4 .= ' WHERE a1.parentid = 0 AND a1.level = 1 AND a2.`id` NOT IN ( ' . $openChildAreaIdString . ' ) ';
        }
        $upd1 = DB::update($sql1);
        $upd2 = DB::update($sql2);
        $upd3 = DB::update($sql3);
        $upd4 = DB::update($sql4);

        return (is_int($upd1) && is_int($upd2)) || (is_int($upd3) && is_int($upd4));
    }

    public function updSettings($configName, $configValue)
    {
        $upd = static::where(['config_name' => $configName])->update(['config_value' => $configValue]);

        return is_int($upd);
    }

    public function addSettings($configName, $configValue)
    {
        return static::insert(['config_name' => $configName, 'config_value' => $configValue]);
    }

    public function sysSettingExists($configName)
    {
        $where = [
            'config_name' => $configName,
        ];

        return static::where($where)->exists();
    }
}
